package com.sunyard.domee.service.impl;

import com.github.pagehelper.Page;

import com.sunyard.domee.common.enums.DelFlagEnum;
import com.sunyard.domee.common.model.PageHelperAdaptor;
import com.sunyard.domee.common.model.PageListResult;
import com.sunyard.domee.commons.constants.Contants;
import com.sunyard.domee.dal.dao.MarketingCouponDAO;
import com.sunyard.domee.dal.dataobject.MarketingCouponDO;
import com.sunyard.domee.dal.query.MarketingCouponQuery;
import com.sunyard.domee.service.MarketingCouponService;
import com.sunyard.domee.service.bo.MarketingCouponBO;
import com.sunyard.domee.service.convert.MarketingCouponConvert;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


/**
 * 优惠券
 *
 * @author xw
 * @email wei.xiao  çæææ
 * @date 2020-07-02 22:52:45
 */
@Service
public class MarketingCouponServiceImpl implements MarketingCouponService {

	@Resource
	private MarketingCouponDAO marketingCouponDAO;


    @Override
    public int add(MarketingCouponBO marketingCouponBO){
		MarketingCouponDO marketingCouponDO = MarketingCouponConvert.toDO(marketingCouponBO);
		return marketingCouponDAO.insert(marketingCouponDO);
    }

    @Override
    public int update(MarketingCouponBO marketingCouponBO){
		MarketingCouponDO marketingCouponDO = MarketingCouponConvert.toDO(marketingCouponBO);
        return marketingCouponDAO.updateByPrimaryKeySelective(marketingCouponDO);
    }

	@Override
	public PageListResult<MarketingCouponBO> findPage(MarketingCouponQuery pagerCondition){
		//分页页码设置
		pagerCondition.setOrderBy(Contants.ORDER_BY);
		Page page = PageHelperAdaptor.preparePage(pagerCondition, Boolean.TRUE);
		List<MarketingCouponDO> list = marketingCouponDAO.selectByExample(this.convertExample(pagerCondition));
//		List<MarketingCouponDO> list = marketingCouponDAO.select(convert(pagerCondition));
		//结果集设置
		PageListResult<MarketingCouponBO> pageListResult = new PageListResult(MarketingCouponConvert.toBOList(list));
		PageHelperAdaptor.setPageResult(page, pageListResult);
		pageListResult.setPageNum(pagerCondition.getPageNum());
		pageListResult.setPageSize(pagerCondition.getPageSize());
		return pageListResult;
	}

	@Override
	public List<MarketingCouponBO> findList(MarketingCouponQuery pagerCondition){
		List<MarketingCouponDO> list = marketingCouponDAO.selectByExample(convertExample(pagerCondition));
		return MarketingCouponConvert.toBOList(list);
	}

	@Override
	public MarketingCouponBO findById(Long id) {
		MarketingCouponDO marketingCouponDO = marketingCouponDAO.selectByPrimaryKey(id);
		return MarketingCouponConvert.toBO(marketingCouponDO);
	}


	private Example convertExample(MarketingCouponQuery pagerCondition){
    	Example example = new Example(MarketingCouponDO.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("delFlag", DelFlagEnum.NOT_DEL.getCode());
		if(!ObjectUtils.isEmpty(pagerCondition.getId())){
			criteria.andEqualTo("id", pagerCondition.getId());
		}
		if(!CollectionUtils.isEmpty(pagerCondition.getIds())){
			criteria.andIn("id", pagerCondition.getIds());
		}
		if(!ObjectUtils.isEmpty(pagerCondition.getCouponName())){
			criteria.andEqualTo("couponName", pagerCondition.getCouponName());
		}
		if(!ObjectUtils.isEmpty(pagerCondition.getCouponStatus())){
			criteria.andEqualTo("couponStatus", pagerCondition.getCouponStatus());
		}
		if(!ObjectUtils.isEmpty(pagerCondition.getFindDate())){
			criteria.andGreaterThanOrEqualTo("effectiveEndDate", pagerCondition.getFindDate());
			criteria.andLessThanOrEqualTo("effectiveStartDate", pagerCondition.getFindDate());
		}

		return example;
	}

}
